//
//  ViewController.m
//  Kugou-OC
//
//  Created by BH on 16/1/21.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "ViewController.h"
#import "HomeController.h"
#import "BottomPlayView.h"
#import "PlayController.h"
#import "PlayHelper.h"
#import "PlaylistView.h"
#import "GlobalDefine.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>

@interface ViewController ()<BottomPlayViewDelegate>{
    UIWindow *window;
}

@property(nonatomic,strong)BottomPlayView *bottomPlay;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置根视图
    window = [[UIApplication sharedApplication].delegate window];
    
    HomeController *home = [[HomeController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:home];
    home.navigationController.navigationBarHidden = YES;
    window.rootViewController = nav;
    
    //添加底部工具栏
    _bottomPlay = [BottomPlayView shareBottomView];
    _bottomPlay.delegate = self;
    _bottomPlay.musicList = [[PlayHelper sharePlayHelper] getLocalMusic];
    [window addSubview:_bottomPlay];
    _bottomPlay.sd_layout
    .leftSpaceToView(window,0)
    .rightSpaceToView(window,0)
    .bottomSpaceToView(window,0)
    .heightIs(90);
    
    _bottomPlay.TapBottomView = ^(NSArray *array,AVPlayerItem *item,NSInteger index){
    
        PlayController *playCtrl = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PlayController"];
        playCtrl.singsList = array;
        playCtrl.playerItem = item;
        playCtrl.currentIndex = index;
        [window addSubview:playCtrl.view];
        [window bringSubviewToFront:playCtrl.view];
    };
}

#pragma mark - BottomPlayView delegate
-(void)showPlayListViewWithItem:(AVPlayerItem *)item array:(NSArray *)array index:(NSInteger)index hidden:(BOOL)hidden{
    
    PlaylistView *playListV = [[PlaylistView alloc] init];
    playListV.playList = array;
    playListV.currentIndex = index;
    playListV.currentItem = item;
    playListV.Play = ^(NSArray *playList,NSInteger playIndex){
        _bottomPlay.musicList = playList;
        [_bottomPlay setupPlayerWithModel:playList[playIndex]];
    };
    
    if (array.count<5) {
        playListV.frame = CGRectMake(0, SCREEN_HEIGHT-70, SCREEN_WIDTH, array.count*60+44);
    }else{
        playListV.frame = CGRectMake(0, SCREEN_HEIGHT-70, SCREEN_WIDTH, 5*60+44);
    }
    
    if (!hidden) {
        [window addSubview:playListV];
        //显示
        [UIView animateWithDuration:0.5 animations:^{
            [window bringSubviewToFront:_bottomPlay];
            playListV.transform = CGAffineTransformMakeTranslation(0, -playListV.height);
        }];
        
    }else{
        
        for (UIView *view in window.subviews) {
            if ([view isKindOfClass:[PlaylistView class]]) {
                //隐藏
                [UIView animateWithDuration:0.5 animations:^{
                    view.transform = CGAffineTransformIdentity;
                    
                } completion:^(BOOL finished) {
                    [view removeFromSuperview];
                }];
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
